package edu.ustb.services.weatherforecast.contract;

import edu.ustb.sc.definitions.*;

import java.util.*;

public class ServiceContract extends ContractTemplate {

    public ContractPartyInfo consumer = new ServiceContractParty("Customer");
    public ContractPartyInfo provider = new ServiceContractParty("Provider");

    public ContractData data;

    public ServiceContract() {

        data = new ContractData();

        parties = new ArrayList<>();
        parties.add(consumer);
        parties.add(provider);

        termName2Action = new HashMap<>();
        termName2Action.put("ServiceTerm3_1", "request");
        termName2Action.put("ServiceTerm3_2", "offer");

        canTermNames = new ArrayList<>();
        canTermNames.add("ServiceTerm3_1");
        mustTermNames = new ArrayList<>();
        mustTermNames.add("ServiceTerm3_2");
        cannotTermNames = new ArrayList<>();
        didMap = new HashMap<>();

    }


    public boolean request(String partyName, String service){
        data.requestMap.put(partyName, service);
        didMap.put("request",true);
        return true;
    }

    public boolean offer(){
        System.out.println("provider 提供服务");
        didMap.put("offer",true);
        return true;
    }


    @Override
    public String getAvailableActions(String partyName) {
        return null;
//        StringBuilder strBuilder = new StringBuilder();
//        strBuilder.append(partyName + ": \n");
//        ContractActionInfo mustDoAction = getUrgentMustTermAction();
//        System.out.println("mustDoAction:  "+mustDoAction);
//
//        for (ContractTermEnum termEnum : ContractTermEnum.values()) {
//            String actionName = termEnum.SpescTerm.getActionName();
//            switch (termEnum) {
//                case Term1:
//                    if (partyName.equals(consumer.getPartyName()) ) {
//                        if (termEnum.SpescTerm.whenPreCondition(this) == SpescConditionCheckStatus.Yes) {
//                            strBuilder.append(partyName + " can " +
//                                    termEnum.SpescTerm.getActionInfo().getAction().getName() + " \n");
//                        }
//                    }
//                    break;
//                case Term2:
//                    if (partyName.equals(provider.getPartyName())) {
////                        for (String ticketName : contractData.getTicketNumber().keySet())
////                            if( didMap.getOrDefault(actionName,false)!=true ){
////                                if (termEnum.SpescTerm.whenPreCondition(this,ticketName) == SpescConditionCheckStatus.Yes) {
////                                    strBuilder.append(partyName + " can " +
////                                            termEnum.SpescTerm.getActionInfo().getAction().getName() + " \n");
////                                }
////                            }
//                    }
//                    break;
//
//                default:
//                    break;
//            }
//        }
//
//        System.out.println(strBuilder.toString());
//        return strBuilder.toString();
    }

    @Override
    protected SpescContractTerm termNameMatchObj(String termName) {
        if (termName.equals("ServiceTerm3_1")) {
            return new ServiceTerm3_1();
        } else if (termName.equals("ServiceTerm3_2")) {
            return new ServiceTerm3_2();
        }
        return null;
    }

    @Override
    public ContractActionInfo getUrgentMustTermAction() {
//        Stack<ContractActionInfo> urgentMustTermActions = new Stack<>();
//        HashSet<ContractActionInfo> urgentCannotTermActions = new HashSet<>();
//        // search all mandatory terms to get urgent must
//        List<ContractTermEnum> mustTermEnums = new ArrayList<>();
//        List<ContractTermEnum> cannotTermEnums = new ArrayList<>();
//
//
//        for (ContractTermEnum termEnum : ContractTermEnum.values()) {
//            if (termEnum.SpescTerm.getQualifier() == SpescTermActionQualifierEnum.Must) {
//                mustTermEnums.add(termEnum);
//            } else if (termEnum.SpescTerm.getQualifier() == SpescTermActionQualifierEnum.Cannot) {
//                cannotTermEnums.add(termEnum);
//            }
//        }
//
//        for (ContractTermEnum termEnum : mustTermEnums) {
//            switch (termEnum) {
//                case Term2:
//                    // TODO: 在某个地方绑定 mustTerms 和对应的Term
//                    if(!didMap.getOrDefault("offer",false)){
//                            ServiceTerm3_2 Term2 = new ServiceTerm3_2();
//                            if (Term2.whenPreCondition(this) == SpescConditionCheckStatus.Yes) {
//                                urgentMustTermActions.add(Term2.getActionInfo());
//
//                        }
//                    }
//                    break;
//                default:
//                    break;
//            }
//        }
//
//        for (ContractActionInfo cannotActionInfo : urgentCannotTermActions) {
//            if (urgentMustTermActions.contains(cannotActionInfo)) {
//                return null;    // must and cannot confliction
//            }
//        }
//        //if (urgentCannotTermActions.isEmpty()) return null;
//        if(!urgentMustTermActions.isEmpty()){
//            return urgentMustTermActions.peek();
//        }
        return null;
    }


    @Override
    public SpescConditionCheckStatus checkPostConditionOfMatchedTermObjs(String partyId, SpescContractTerm termObj) {
        SpescConditionCheckStatus checkStatus = SpescConditionCheckStatus.Unknown;
        ContractTermEnum termEnum = ContractTermEnum.match(termObj.getTermName());
        checkStatus = termObj.wherePostCondition(this);
        return checkStatus;
    }
}
